package com.paging.service.impl;

import com.paging.mapper.AuthorMapper;
import com.paging.mapper.BookAuthorMapper;
import com.paging.pojo.Author;
import com.paging.service.AuthorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 位置：SSM/com.paging.service.impl
 * 时间：2023-04-24-19:13
 * 作者：Administrator
 **/

@Service
public class AuthorServiceImpl implements AuthorService {

    @Autowired
    AuthorMapper authorMapper;

    @Autowired
    BookAuthorMapper bookAuthorMapper;


    @Override
    public List<Author> queryAuthorList() {
        return authorMapper.queryAuthorList();
    }

    @Override
    public int countAuthor(String authorName, String authorSex) {
        return authorMapper.countAuthor(authorName, authorSex);
    }

    @Override
    public int addAuthor(Author author) {
        if (getAuthorName(author)) return 0;
        author.setBookCount(0);
        return authorMapper.addAuthor(author);
    }

    private boolean getAuthorName(Author author) {
        String authorName = author.getAuthorName();
        List<Author> authorList = authorMapper.queryAuthorList();
        if (authorList != null && authorList.size() > 0) {
            for (Author author1 : authorList) {
                if (author1.getAuthorName().equals(authorName) && author1.getId() != author.getId()) {
                    return true;
                }
            }
        }
        return false;
    }

    @Override
    public int deleteAuthor(int id) {
//       删除作者时，需要先判定该作者是否有书籍，如果有书籍，则不能删除
        String authorId = id + "";
        String[] numList = bookAuthorMapper.queryBookIdByAuthorId(authorId);
        if (numList != null && numList.length > 0) {
            return 0;
        }
        return authorMapper.deleteAuthor(id);
    }

    @Override
    public int updateAuthor(Author author) {
//        对作者进行修改时，需要判断是否有重名的作者
        if (getAuthorName(author)) return 0;
        return authorMapper.updateAuthor(author);
    }

    @Override
    public int deleteAuthorByIds(String idList) {
        //  删除作者时，需要先判定该作者是否有书籍，如果有书籍，则不能删除
        String[] numList = idList.split(",");
            for (String id : numList) {
                String authorId = id + "";
                String[] bookIdList = bookAuthorMapper.queryBookIdByAuthorId(authorId);
                if (bookIdList != null && bookIdList.length > 0) {
                    return 0;
                }
            }
        idList = idList.substring(0, idList.length() - 1);
        return authorMapper.deleteAuthorByIds(idList);
    }



    @Override
    public List<Author> queryAuthorListByPage(Integer start, Integer pageSize, String authorName, String authorSex) {
        if(start != null){
            if(start > 1){
                start = (start - 1) * pageSize;
            }else{
                start = 0;
            }
        }


//        List<Author> authorList = authorMapper.queryAuthorList();
//        if (authorList != null && authorList.size() > 0) {
//            for (Author author : authorList) {
//                String authorId = author.getId() + "";
//                String[] bookIdList = bookAuthorMapper.queryBookIdByAuthorId(authorId);
//                if (bookIdList != null && bookIdList.length > 0) {
//                    author.setBookCount(bookIdList.length);
//                } else {
//                    author.setBookCount(0);
//                }
//            }
//        }


        return authorMapper.queryAuthorListByPage(start,pageSize,authorName,authorSex);
    }

    @Override
    public String queryAuthorIdByName(String authorName) {
        return authorMapper.queryAuthorIdByName(authorName);
    }

}
